package com.student_exam_system.service.impl;

import com.student_exam_system.dao.UserMapper;
import com.student_exam_system.pojo.LoginUser;
import com.student_exam_system.pojo.User;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author fire'ge
 */
@Service("userDetailServiceImpl")
public class UserDetailServiceImpl implements UserDetailsService {
    private final UserMapper userMapper;

    public UserDetailServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }


    @Override
    public UserDetails loadUserByUsername(String phoneNum) throws UsernameNotFoundException {
        User user = userMapper.queryUserByUsername(phoneNum);
        if(Objects.isNull(user)) {
            throw new RuntimeException("用户名或者密码错误");
        }
        List<String> list = new ArrayList<>(Arrays.asList("teacher","student"));
        return new LoginUser(user,list);
    }
}
